package com.xudow.app.dynamicstate_old.module.follow;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.GsonBuilder;
import com.jude.beam.expansion.BeamBaseActivity;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.domain.entity.DynamicComment;
import com.xudow.app.dynamicstate_old.widget.ContainsEmojiEditText;
import com.xudow.app.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ske on 2016/6/13.
 */
public class CommentActivity extends BeamBaseActivity {
    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.comment)
    TextView comment;
    @BindView(R.id.edit_comment)
    ContainsEmojiEditText editComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_comment);
        ButterKnife.bind(this);

        app = (XApplication) getApplication();
        setupView();
    }

    XApplication app;

    Dynamic dynamic;

    private void setupView() {
        dynamic = (Dynamic) getIntent().getSerializableExtra("dynamic");
        final DynamicComment originComment = (DynamicComment) getIntent().getSerializableExtra("dynamicComment");
        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editComment.getText().toString().trim())) {
                    Toast.makeText(CommentActivity.this, "请填写内容", Toast.LENGTH_SHORT).show();
                } else {
                    DynamicComment dynamicComment;
//                    没有父评论

                    if (originComment == null) {
                        dynamicComment = new DynamicComment(dynamic.getId(),
                                editComment.getText().toString().trim(),
                                0, "", app.getUserProfileId(),
                                app.isStudent() ? 2 : 1,
                                dynamic.getUsertype());
                    } else {
                        dynamicComment = new DynamicComment(dynamic.getId(),
                                editComment.getText().toString().trim(),
                                originComment.getId(), originComment.getUserName(), app.getUserProfileId(),
                                app.isStudent() ? 2 : 1,
                                dynamic.getUsertype());
                    }

                    DynamicModel.getInstance().commentDynamic(
                            new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
                                    .create().toJson(dynamicComment))
                            .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                            .subscribe(message -> {
                                setResult(RESULT_OK);
                                finish();
                            });
                }
            }
        });
        editComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(s.toString())) {
                    comment.setTextColor(Color.GRAY);
                } else {
                    comment.setTextColor(Color.RED);
                }
            }
        });
    }
}
